package com.redisdemo.File;

import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * <h3>redisDemo</h3>
 * <p>测试类</p>
 *
 * @author gdw
 * @since 2024-07-02 15:16:16
 */
public class MultiThreadedFileReader {
    public static void main(String[] args) {
        //readFile();  //读取文件
        //copyFile();  //复制文件覆盖模式
        copyZjFile(); //复制文件追加模式
    }

    /**
     * 复制文件,追加模式
     */
    public static void copyZjFile(){
        String sourceFilePath = "D:\\abooster_start.sql";
        String destFilePath = "D:\\ceshi2.txt";
        int numThreads = 6; // 可以根据需要调整线程数量

        File sourceFile = new File(sourceFilePath);
        long fileSize = sourceFile.length();
        long chunkSize = fileSize / numThreads;

        ExecutorService executor = Executors.newFixedThreadPool(numThreads);

        long startTime = System.nanoTime(); // 记录开始时间

        for (int i = 0; i < numThreads; i++) {
            long start = i * chunkSize;
            long size = (i == numThreads - 1) ? (fileSize - start) : chunkSize;
            executor.execute(new FileCopyTask(sourceFilePath, destFilePath, start, size));
        }

        executor.shutdown();
        while (!executor.isTerminated()) {
            // 等待所有任务完成
        }

        long endTime = System.nanoTime(); // 记录结束时间

        long duration = (endTime - startTime) / 1_000_000; // 计算所用时间，单位为毫秒
        System.out.println("Total time taken: " + duration + " ms");
    }


    /**
     * 复制文件,覆盖模式
     */
    public static void copyFile(){
        String sourceFilePath = "D:\\abooster_start.sql";
        String destFilePath = "D:\\ceshi.sql";
        int numThreads = 6; // 可以根据需要调整线程数量

        File sourceFile = new File(sourceFilePath);
        long fileSize = sourceFile.length();
        long chunkSize = fileSize / numThreads;

        ExecutorService executor = Executors.newFixedThreadPool(numThreads);

        long startTime = System.nanoTime(); // 记录开始时间

        for (int i = 0; i < numThreads; i++) {
            long start = i * chunkSize;
            long size = (i == numThreads - 1) ? (fileSize - start) : chunkSize;
            executor.execute(new FileCopyTask(sourceFilePath, destFilePath, start, size));
        }

        executor.shutdown();
        while (!executor.isTerminated()) {
            // 等待所有任务完成
        }

        long endTime = System.nanoTime(); // 记录结束时间

        long duration = (endTime - startTime) / 1_000_000; // 计算所用时间，单位为毫秒
        System.out.println("Total time taken: " + duration + " ms");
    }
    /**
     * 读取文件
     */
    public static void readFile(){
        String filePath = "D:\\abooster_start.sql";
        int numThreads = 6; // 可以根据需要调整线程数量

        File file = new File(filePath);
        long fileSize = file.length();
        long chunkSize = fileSize / numThreads;
        long startTime = System.nanoTime(); // 记录开始时间
        ExecutorService executor = Executors.newFixedThreadPool(numThreads);
        for (int i = 0; i < numThreads; i++) {
            long start = i * chunkSize;
            long size = (i == numThreads - 1) ? (fileSize - start) : chunkSize;
            executor.execute(new FileReadTask(filePath, start, size));
        }

        executor.shutdown();
        while (!executor.isTerminated()) {
            // 等待所有任务完成
        }

        long endTime = System.nanoTime(); // 记录结束时间

        long duration = (endTime - startTime) / 1_000_000; // 计算所用时间，单位为毫秒
        System.out.println("Total time taken: " + duration + " ms");
    }
}
